package com.aruba.paging3.paging.model;

import androidx.annotation.NonNull;
import androidx.paging.ItemKeyedDataSource;

import com.aruba.paging3.api.RetrofitClient;
import com.aruba.paging3.entity.Movie;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by aruba on 2021/9/17.
 */
public class MovieDataSource extends ItemKeyedDataSource<Integer, Movie> {
    public static final int PER_PAGE = 8;

    @Override
    public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Movie> callback) {
        RetrofitClient.getInstance().getApi()
                .getMovies(0, PER_PAGE)
                .enqueue(new Callback<List<Movie>>() {
                    @Override
                    public void onResponse(Call<List<Movie>> call, Response<List<Movie>> response) {
                        if (response.body() != null)
                            callback.onResult(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<Movie>> call, Throwable t) {

                    }
                });
    }

    @Override
    public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Movie> callback) {
        RetrofitClient.getInstance().getApi()
                .getMovies(params.key, PER_PAGE)
                .enqueue(new Callback<List<Movie>>() {
                    @Override
                    public void onResponse(Call<List<Movie>> call, Response<List<Movie>> response) {
                        if (response.body() != null)
                            callback.onResult(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<Movie>> call, Throwable t) {

                    }
                });
    }

    @Override
    public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Movie> callback) {

    }

    @NonNull
    @Override
    public Integer getKey(@NonNull Movie item) {
        return item.id;
    }
}
